package hy;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class EsDoc {
    RestHighLevelClient cli;
    @Before
    public void init() {
        cli=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.188.100",9200,"http"))
        );
    }
    @After
    public void end()throws Exception{
        cli.close();
    }

    @Test
    public void insertData()throws Exception{
        IndexRequest req=new IndexRequest().index("user").id("1");
        User u=new User();
        u.setName("this is test name");
        u.setAge(120);
        String json = JSON.toJSONString(u);
        req.source(json, XContentType.JSON);
        IndexResponse resp = cli.index(req, RequestOptions.DEFAULT);
        System.out.println(resp.getResult());
    }
    @Test
    public void updateData()throws Exception{
        UpdateRequest req=new UpdateRequest().index("user").id("1");
        req.doc(XContentType.JSON,"name","hello,it is test");
        UpdateResponse resp = cli.update(req, RequestOptions.DEFAULT);
        System.out.println(resp.getResult());
    }
    @Test
    public void queryData()throws Exception{
        GetRequest req = new GetRequest().index("user").id("2");
        GetResponse resp = cli.get(req, RequestOptions.DEFAULT);
        System.out.println(resp.getSourceAsString());
    }
    @Test
    public void deleteData()throws Exception{
        DeleteRequest req=new DeleteRequest().index("user").id("2");
        DeleteResponse resp = cli.delete(req, RequestOptions.DEFAULT);
        System.out.println(resp.toString());
    }
    @Test
    public void batchInsertData()throws Exception{
        //产生批处理数据
        BulkRequest batchReq=new BulkRequest();
        batchReq.add(new IndexRequest().index("user").id("2").source(XContentType.JSON,"age",33,"name","a3"));
        batchReq.add(new IndexRequest().index("user").id("3").source(XContentType.JSON,"age",34,"name","a4"));
        batchReq.add(new IndexRequest().index("user").id("4").source(XContentType.JSON,"age",35,"name","a5"));
        batchReq.add(new IndexRequest().index("user").id("5").source(XContentType.JSON,"age",36,"name","a6"));
        //向客户端添加数据并请求写入
        BulkResponse resp = cli.bulk(batchReq, RequestOptions.DEFAULT);
    }
}
@Data
class User{
    String name;
    int age;
}